package map;

import java.util.*;

public class TestDemo {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        list.add(new User("张三",23));
        list.add(new User("李四",21));
        list.add(new User("王五",23));
        /*list.sort(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getAge()-o2.getAge();
            }
        });*/
        list.sort(((o1, o2) -> o1.getAge()- o2.getAge()));
        System.out.println(list);
        Map<Integer,List<String>> userMap = new HashMap<>();
        for (User user : list){
            List<String> al;
            if (userMap.containsKey(user.getAge())){
                al = userMap.get(user.getAge());
            }else {
                al = new ArrayList<>();
            }
            al.add(user.getUserName());
            userMap.put(user.getAge(), al);
        }
        System.out.println(userMap);
    }
}
class User{
    private String userName;
    private int age;

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
}
